package com.travel.ticket.web;

import com.alibaba.fastjson.JSON;
import com.travel.common.utils.R;
import com.travel.common.vo.MemberRespVo;
import com.travel.ticket.service.CollectionService;
import com.travel.ticket.service.SkuInfoService;
import com.travel.ticket.service.TicketService;
import com.travel.ticket.vo.SpuInfoVo;
import com.travel.ticket.vo.TicketVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import static com.travel.common.constant.AuthServerConstant.LOGIN_USER;

/**
 * 处理门票
 */
@Controller
public class TicketController {

    @Autowired
    TicketService ticketService;

    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    SkuInfoService skuInfoService;

    @Autowired
    CollectionService collectionService;

    /**
     * 保存spu和所有sku信息
     * @param spuInfoVo
     * @return
     */
    @ResponseBody
    @PostMapping("/ticket/saveSpuAndSkus")
    public R saveSpuAndSkus(@RequestBody SpuInfoVo spuInfoVo){
        System.out.println(spuInfoVo.toString());
        ticketService.saveSpuAndSkus(spuInfoVo);

        return R.ok();
    }

    /**
     * 查看详情
     */
    @GetMapping("/{spuId}.html")
    public String ticketInfo(@PathVariable("spuId")Integer spuId, Model model){
        //1. 获取景点信息
        TicketVo ticketVo = ticketService.info(spuId);
        model.addAttribute("ticket",ticketVo);

        //2. 获取当前用户是否对当前票务有 收藏
        //2.1 从redis获取用户信息
        String s = redisTemplate.opsForValue().get(LOGIN_USER);
        MemberRespVo memberRespVo = JSON.parseObject(s,MemberRespVo.class);
        Integer userId = memberRespVo.getMemberId().intValue();

        //3. 从redis中获取当前用户
        String user = redisTemplate.opsForValue().get(LOGIN_USER);
        MemberRespVo loginUser = JSON.parseObject(user, MemberRespVo.class);
        //TODO  4. 获取收藏情况
        Boolean isCollection = collectionService.isCollection(spuId, userId);
        model.addAttribute("isCollection", isCollection);

        model.addAttribute("loginUser",loginUser);
        model.addAttribute("spuId",spuId);
        return "ticketInfo";
    }

    /**
     * 获取指定sku的价格
     */
    @PostMapping("/getSkuPrice")
    @ResponseBody
    public R getSkuPrice(@RequestParam Integer spuId,
                         @RequestParam String selectedAttrs,
                         @RequestParam String selectedDate){
        System.out.println("spuId==="+spuId);
        System.out.println("selectedAttrs==="+selectedAttrs);
        System.out.println("selectedDate==="+selectedDate);



        BigDecimal price = skuInfoService.getSkuPrice(spuId,selectedAttrs);
        price.setScale(0, RoundingMode.DOWN);
        //TODO 可以根据日期来调整价格

        return R.ok().put("price",price);
    }
}
